package com.download.zhuaba.Controller;

import com.alibaba.fastjson.JSON;
import com.download.zhuaba.Dao.HistoryRepository;
import com.download.zhuaba.Dao.UserRepository;
import com.download.zhuaba.Entity.History;
import com.download.zhuaba.Entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HistoryController {
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private UserRepository userRepository;
    @GetMapping("/History.html")
    public String history(Model model, HttpServletRequest request, HttpSession session){
        if(request.getSession().getAttribute("loginUser")!=null){
            int userId =Integer.valueOf(request.getSession().getAttribute("loginUser").toString());
            List<History> histories=new ArrayList<History>();
            histories =historyRepository.findAll();
            ArrayList<History> histories1 =new ArrayList<>();
            if(histories!=null){
                for(History history:histories){
                  if(history.getUserid()==userId){
                    histories1.add(history);
                  }
                }
            }
            String jsonList = JSON.toJSONString(histories1);
            session.setAttribute("flag", "1");//跳转地址不带上u2参数
            session.setAttribute("login","1");
            model.addAttribute("htmlCode2",jsonList);
            return "History";
        }
        else{
            session.setAttribute("flag", "1");//跳转地址不带上u2参数
            session.setAttribute("login","0");
            return "login";
        }
    }
    @ResponseBody
    @RequestMapping(value = "/News/saveJsonHistory",method = RequestMethod.POST)
    public String historyAdmin(@RequestParam("href") String href, @RequestParam("time") String time,@RequestParam("locationhref") String locationhref,HttpSession session,HttpServletRequest request){
        History history = new History();
        history.setHref(href);
        history.setTime(time);
        history.setUserid(Integer.valueOf(request.getSession().getAttribute("loginUser").toString()));
        System.out.println(Integer.valueOf(request.getSession().getAttribute("loginUser").toString()));
        historyRepository.save(history);
        session.setAttribute("flag", "1");//跳转地址不带上u2参数
        session.setAttribute("login","1");
        return "SUCCESS";
    }
    @ResponseBody
    @RequestMapping(value = "/Video/saveJsonHistory",method = RequestMethod.POST)
    public String historyAdmin1(@RequestParam("href") String href, @RequestParam("time") String time,@RequestParam("locationhref") String locationhref,HttpSession session,HttpServletRequest request){
        History history = new History();
        history.setHref(href);
        history.setTime(time);
        history.setUserid(Integer.valueOf(request.getSession().getAttribute("loginUser").toString()));
        historyRepository.save(history);
        session.setAttribute("flag", "1");//跳转地址不带上u2参数
        session.setAttribute("login","1");
        return "SUCCESS";
    }
}
